<?php
namespace app\interfaces\controller;
use think\Db;
use think\Session;
use libs\Easemob;
class Huanxin extends Base{
private $ms;
public function _initialize()
{
	$ms=Db::table('hxconfig')->where(array('id'=>1))->find();
	$options['client_id'] = $ms['client_id'];  
    $options['client_secret'] = $ms['client_secret'];  
    $options['org_name'] = $ms['org_name'];  
    $options['app_name'] = $ms['app_name'];
	$options['access_token'] = $ms['access_token'];
	$options['expires_in'] = $ms['expires_in'];
	$options['updateTime'] = $ms['updateTime'];
	//var_dump($options);
	//exit;
    $this->ms=new Easemob;
    $this->ms->index($options);
    //$ms->getToken();	
}
public function registerhx($userid)
{
	
	//$username='liming';
	//$password=123456;
	$id=$userid;
	//$ros=Db::table('user')->where(array('id'=>$id))->find();
	//var_dump($ros);
	//exit;
	$username='hx'.$id;
	$password=md5(123456);
	$mm=$this->ms->createUser($username,$password);
	$data=array(
	'uid'=>$id,
	'hxuname'=>$mm['entities'][0]['username'],
	'hxpawd'=>$password,
	'hxuuid'=>$mm['entities'][0]['uuid'],
	'type'=>$mm['entities'][0]['type']
	);
	$tm=Db::table('hxuser')->insert($data);
	if($tm)
	{
		Db::table('user')->where(array('id'=>$id))->update(array('ishx'=>1));
	}
	//echo "注册成功";
	//exit;
	//var_dump($mm);
	//exit;
	
}
//查询用户
public function finduser()
{
	$username=input('username');
	$ms=$this->ms->getUser($username);
	if(!empty($ms['error']))
	{
		echo "没有此用户";
		exit;
	}
	echo $ms['entities'][0]['username'];
	exit;
	//var_dump($ms);
	//exit;
}
//添加好友
public function addfind()
{
	$username='15838056141';
	$friend_name='zhangningn';
	$ss=$this->ms->addFriend($username,$friend_name);
	if(!empty($ss['erroe']))
	{
		echo '添加失败';
		exit;
	}
	echo "添加成功";
	//var_dump($ss);
	exit;
}
//查看好友
public function showfind()
{
	$username='15838056141';
	$ss=$this->ms->showFriends($username);
	if(!empty($ss['erroe']))
	{
		echo '查询失败';
		exit;
	}
	if(empty($ss['entities']['data']))
	{
		echo "暂无好友";
		exit;
	}
	
	var_dump($ss['entities']['data']);
	exit;
}

}
?>